package pl.kane.autokomp.livegenerator;

import java.awt.AWTException;

import java.awt.Robot;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;

import pl.kane.autokomp.livegenerator.dto.MyClassLoader;







public class Generator {
	
	String rootPath;

	
	public Generator(){
		rootPath = getClass().getResource("/").getFile();
	}
	
	public String generate(String body){
		return AutoKompParser.runParser(body);
	}

	public void writeClass(String className, String body) throws IOException{
		String pakiet = className.substring(0, className.lastIndexOf('.')).replace('.','/');
		String klasa = className.substring(className.lastIndexOf('.')+1);
		File dir = new File(rootPath, pakiet);
		dir.mkdirs();
		File f = new File(dir, klasa+".java");
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		bw.write(body);
		bw.close();
	}
	
	public void compileClass(String className){
		com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main(); //(System.out, "javac");
		String url = getClass().getResource("/pl/kane/autokomp/livegenerator/Generator.class").getFile();
		if (url.indexOf('!')>=0){
			// jar
			url = url.substring(6,url.indexOf('!'));
	
		}else{
			// klasy
			url = url.substring(0, url.length()-"/pl/kane.autokomp/livegenerator/Generator.class".length());
		}
		String seperator = System.getProperty("path.separator");
		javac.compile(new String[]{"-nowarn","-sourcepath",rootPath,"-classpath",url+seperator+rootPath,"-d", rootPath, rootPath+"/"+className.replace('.','/')+".java"});
	}
	
	public Generic load(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		ClassLoader parentClassLoader = MyClassLoader.class.getClassLoader();
	    MyClassLoader classLoader = new MyClassLoader(parentClassLoader, getClass().getResource("/").getFile(),className);
	    Class myObjectClass = classLoader.loadClass(className);
	    return (Generic) myObjectClass.newInstance();
//		return	(Generic)Class.forName(className, true, loader).newInstance();

	}
	
	public void run(String body) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		Runtime r = Runtime.getRuntime(); 
        r.gc(); 

		String program = "pl.kane.autokomp.livegenerator.Program";
		
		writeClass(program, generate(body));
		compileClass(program);
		Generic sumator = load(program);
		
		try {
			sumator.execute();
			
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		Generator gen = new Generator();
		
		try {
			gen.run("move(10,10);end");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
